﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;

// 订单明细类
class OrderDetail
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public override string ToString()
    {
        return $"Product: {ProductName}, Price: {Price}, Quantity: {Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public double TotalAmount
    {
        get { return Details.Sum(detail => detail.Price * detail.Quantity); }
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("Order already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    public IEnumerable<Order> QueryByCustomer(string customer)
    {
        return orders.Where(order => order.Customer == customer);
    }

    public void SortOrdersByTotalAmount()
    {
        orders = orders.OrderBy(order => order.TotalAmount).ToList();
    }

    public void Export(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Create))
        {
            serializer.Serialize(fs, orders);
        }
    }

    public void Import(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            List<Order> importedOrders = (List<Order>)serializer.Deserialize(fs);
            foreach (var order in importedOrders)
            {
                AddOrder(order);
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        OrderService orderService = new OrderService();

        // 示例：添加订单
        Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail>() };
        orderService.AddOrder(order1);

        // 示例：导出订单到XML文件
        orderService.Export("orders.xml");

        // 示例：从XML文件中导入订单
        OrderService importedOrderService = new OrderService();
        importedOrderService.Import("orders.xml");

        // 示例：查询订单
        IEnumerable<Order> orders = importedOrderService.QueryByCustomer("Alice");
        foreach (var order in orders)
        {
            Console.WriteLine(order);
        }

        // 示例：排序订单
        importedOrderService.SortOrdersByTotalAmount();

        Console.ReadLine();
    }
}

